package whr.modules.ask.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import whr.common.utils.PageUtils;
import whr.common.utils.Query;

import whr.modules.ask.dao.ReplyDao;
import whr.modules.ask.entity.ReplyEntity;
import whr.modules.ask.entity.vo.ReplyVo;
import whr.modules.ask.service.ReplyService;
import whr.modules.sys.dao.SysUserDao;
import whr.modules.sys.entity.SysUserEntity;
import whr.modules.sys.service.SysUserService;


@Service("replyService")
public class ReplyServiceImpl extends ServiceImpl<ReplyDao, ReplyEntity> implements ReplyService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ReplyService replyService;

    @Autowired
    private ReplyDao replyDao;
    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 获取前端传入的查询名字参数
        String realName = (String) params.get("key");
        QueryWrapper queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.eq(StringUtils.isNotBlank(realName),"real_name",realName);
        List<SysUserEntity> list = sysUserService.list(queryWrapper0);
        Long userId1 = list.get(0).getUserId();
        IPage<ReplyEntity> page;

        if (StringUtils.isNotEmpty(realName)) {
            // 如果传入了查询名字，根据名字进行模糊查询
            QueryWrapper<ReplyEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId1); // 假设ReplyEntity中有一个content字段用于存储相关内容，可根据实际情况修改
            page = this.page(
                    new Query<ReplyEntity>().getPage(params),
                    queryWrapper
            );
        } else{
            // 如果没有传入查询名字，按照原来的方式进行查询
            page = this.page(
                    new Query<ReplyEntity>().getPage(params),
                    new QueryWrapper<ReplyEntity>()
            );
        }

        // 遍历page中的每一个ReplyEntity，查询对应的用户名字并设置到ReplyEntityVo中
        List<ReplyEntity> replyEntityList = page.getRecords();
        for (ReplyEntity replyEntity : replyEntityList) {
            Long userId = replyEntity.getUserId();
//            System.out.println("找到的用户信息id  "+userId.getClass());
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            // 如果传入了用户名，添加基于用户名的查询条件
            SysUserEntity userEntity = sysUserService.getOne(queryWrapper);
//            System.out.println("找到的用户信息"+userEntity);
            if (userEntity!= null) {
                replyEntity.setRealName(userEntity.getRealName());
            }
        }
        return new PageUtils(page);
    }

    @Override
    public void saveByUserName(ReplyEntity reply) {
        reply.setAskStatus("0");  //待审核
        replyService.save(reply);

    }

    @Override
    public ReplyEntity getbyI(Long askId) {
        ReplyEntity reply = replyService.getById(askId);
        Long userId = reply.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        SysUserEntity one = sysUserService.getOne(queryWrapper);
        String realName = one.getRealName();
        reply.setRealName(realName);
        return reply;
    }

}